export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerUserForm.passwordCheck !== '') {
          this.$refs.registerUserForm.validateField('passwordCheck')
        }
        callback()
      }
    }
    let validatePassCheck = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerUserForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registerFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [
          { validator: validatePass, message: '请输入密码', trigger: 'blur' }
        ],
        passwordCheck: [{ validator: validatePassCheck, trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName, func) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          func()
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
